package mipo.top.pet.controller;

import mipo.top.admin.domain.Employee;
import mipo.top.admin.service.IEmployeeService;
import mipo.top.base.util.JsonResult;
import mipo.top.base.util.LoginContext;
import mipo.top.base.util.PageUtil;
import mipo.top.pet.Service.IProductService;
import mipo.top.pet.domain.Product;
import mipo.top.pet.query.ProductQuery;
import mipo.top.users.domain.Logininfo;
import mipo.top.users.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductService productService;
    @Autowired
    private IEmployeeService employeeService;

    @GetMapping
    public List<Product> sad(){
        return productService.findall();
    }

    @PutMapping
    public JsonResult su(@RequestBody Product product, HttpServletRequest request){
        try {
            Logininfo loginUser = LoginContext.getLoginUser(request);
            Employee employee = employeeService.findBylogininfoId(loginUser.getId());
            product.setShopId(employee.getShopId());
            if(product.getId()==null){
                productService.save(product);
            }else{
                productService.update(product);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("错误数据");
        }
    }

    @DeleteMapping("/{id}")
    public void adw(@PathVariable("id") Long id){
        productService.remove(id);
    }
    @GetMapping("/{id}")
    public Product findone(@PathVariable("id") Long id){
      return productService.findone(id);
    }

    @PostMapping
    public PageUtil<Product> asp(@RequestBody ProductQuery productQuery){
        return productService.queryData(productQuery);
    }
    @PatchMapping
    private void bacthDel(@RequestBody List<Long> ids){
        productService.patchremove(ids);
    }
    @PostMapping("/offsale")
    private JsonResult offsale(@RequestBody List<Long> ids){
        try{
            productService.offsale(ids);
            return JsonResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        }
    }@PostMapping("/onsale")
    private JsonResult onsale(@RequestBody List<Long> ids){
        try{
            productService.onsale(ids);
            return JsonResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        }
    }
    @GetMapping("/petService/{id}")
    private Product idfindPetService(@PathVariable("id") Long id){
        return productService.idfindPetService(id);
    }@GetMapping("/adopt/{id}")
    private Product buyService(@PathVariable("id") Long id){
        return productService.idfindPetService(id);
    }
}
